/*
 
 * To change this template, choose Tools | Templates
 
 * and open the template in the editor.
 
 */
 
package gui;
 

 
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import bean.Curso;
import bean.Disciplina;
import bean.Professor;
import controlador.ControladorCurso;
import controlador.ControladorDisciplina;
import controlador.ControladorProfessor;
 

 
/**
 
 *
 
 * @author STI
 
 */
 
public class PainelDisciplina extends javax.swing.JPanel {
 

 
    /**
 
     * Creates new form PainelCurso
 
     */
 
    List<Curso> cursos;
 
    List<Professor> professores;
 
    DefaultTableModel tabelaDisciplinas = new DefaultTableModel(new String[]{"Código", "Curso", "Nome", "Carga Horária", "Turma", "Periodo", "Ano", "Serie"}, 0);
 
    int altera =0;
 

 
    public PainelDisciplina() {
 
        initComponents();
 
        preencheComboCurso();
 
        preencheComboProfessor();
 
        preencheTabela();
 
        limparCampos();
 

 
    }
 

 
    public void preencheComboCurso() {
 
        ControladorCurso cc = new ControladorCurso();
 
        cursos = cc.lista();
 
        for (int i = 0; i < cursos.size(); i++) {
 
            Curso c = cursos.get(i);
 
            ComboCurso.addItem(c.getNome());
 
        }
 
    }
 

 
    public void preencheComboProfessor() {
 
        ControladorProfessor cp = new ControladorProfessor();
 
        professores = cp.listar();
 
        for (int i = 0; i < professores.size(); i++) {
 
            Professor p = professores.get(i);
 
            ComboProfessor.addItem(p.getNome());
 
        }
 

 
    }
 

 
    @SuppressWarnings("unchecked")
 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 
    private void initComponents() {
 

 
        buttonGroupPeriodoSerie = new javax.swing.ButtonGroup();
 
        jPanelDisciplina = new javax.swing.JPanel();
 
        jPanelTitulo = new javax.swing.JPanel();
 
        jLabel4 = new javax.swing.JLabel();
 
        jPanelTela = new javax.swing.JPanel();
 
        jPanelCampos = new javax.swing.JPanel();
 
        jLabel3 = new javax.swing.JLabel();
 
        jLabel2 = new javax.swing.JLabel();
 
        jTextFieldNomeDisciplina = new javax.swing.JTextField();
 
        ComboCurso = new javax.swing.JComboBox();
 
        jLabel5 = new javax.swing.JLabel();
 
        jTextFieldCargaHoraria = new javax.swing.JTextField();
 
        jLabel7 = new javax.swing.JLabel();
 
        jTextFieldTurma = new javax.swing.JTextField();
 
        ComboPeriodoSerie = new javax.swing.JComboBox();
 
        jFormattedTextFieldPeriodoSerie = new javax.swing.JFormattedTextField();
 
        jLabel1 = new javax.swing.JLabel();
 
        ComboProfessor = new javax.swing.JComboBox();
 
        tabelaDisciplina = new javax.swing.JScrollPane();
 
        jTableDisciplinas = new javax.swing.JTable();
 
        jPanelBotoes = new javax.swing.JPanel();
 
        jButtonSalvar = new javax.swing.JButton();
 
        jButtonExcluir = new javax.swing.JButton();
 
        jButtonCancelar = new javax.swing.JButton();
 
        jButtonListar = new javax.swing.JButton();
 
        jButtonEditar = new javax.swing.JButton();
 
        jButtonAtualizar = new javax.swing.JButton();
 

 
        setMaximumSize(new java.awt.Dimension(780, 520));
 
        setMinimumSize(new java.awt.Dimension(780, 520));
 
        setPreferredSize(new java.awt.Dimension(780, 520));
 
        setLayout(new java.awt.CardLayout());
 

 
        jPanelDisciplina.setMaximumSize(new java.awt.Dimension(780, 512));
 
        jPanelDisciplina.setMinimumSize(new java.awt.Dimension(780, 512));
 
        jPanelDisciplina.setPreferredSize(new java.awt.Dimension(780, 512));
 

 
        jPanelTitulo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 
        jPanelTitulo.setMaximumSize(new java.awt.Dimension(830, 50));
 
        jPanelTitulo.setMinimumSize(new java.awt.Dimension(830, 50));
 
        jPanelTitulo.setPreferredSize(new java.awt.Dimension(830, 50));
 

 
        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
 
        jLabel4.setText("Disciplina");
 

 
        javax.swing.GroupLayout jPanelTituloLayout = new javax.swing.GroupLayout(jPanelTitulo);
 
        jPanelTitulo.setLayout(jPanelTituloLayout);
 
        jPanelTituloLayout.setHorizontalGroup(
 
            jPanelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelTituloLayout.createSequentialGroup()
 
                .addGap(373, 373, 373)
 
                .addComponent(jLabel4)
 
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
        );
 
        jPanelTituloLayout.setVerticalGroup(
 
            jPanelTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelTituloLayout.createSequentialGroup()
 
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
                .addComponent(jLabel4)
 
                .addContainerGap())
 
        );
 

 
        jPanelTela.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 

 
        jPanelCampos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 

 
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jLabel3.setText("Curso:");
 

 
        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jLabel2.setText("Nome: ");
 

 
        jTextFieldNomeDisciplina.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 

 
        ComboCurso.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 

 
        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jLabel5.setText("Turma:");
 

 
        jTextFieldCargaHoraria.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 

 
        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jLabel7.setText("Carga Horária:");
 

 
        jTextFieldTurma.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 

 
        ComboPeriodoSerie.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        ComboPeriodoSerie.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Período", "Série" }));
 

 
        try {
 
            jFormattedTextFieldPeriodoSerie.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#")));
 
        } catch (java.text.ParseException ex) {
 
            ex.printStackTrace();
 
        }
 
        jFormattedTextFieldPeriodoSerie.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 

 
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jLabel1.setText("Professor: ");
 

 
        javax.swing.GroupLayout jPanelCamposLayout = new javax.swing.GroupLayout(jPanelCampos);
 
        jPanelCampos.setLayout(jPanelCamposLayout);
 
        jPanelCamposLayout.setHorizontalGroup(
 
            jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelCamposLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
                    .addComponent(ComboPeriodoSerie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(jLabel2)
 
                    .addComponent(jLabel3))
 
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
                    .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(jFormattedTextFieldPeriodoSerie, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(ComboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
 
                .addGap(68, 68, 68)
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
                    .addComponent(jLabel1)
 
                    .addComponent(jLabel5)
 
                    .addComponent(jLabel7))
 
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
 
                    .addComponent(jTextFieldTurma, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
 
                    .addComponent(jTextFieldCargaHoraria)
 
                    .addComponent(ComboProfessor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
                .addContainerGap(20, Short.MAX_VALUE))
 
        );
 
        jPanelCamposLayout.setVerticalGroup(
 
            jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelCamposLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
                    .addComponent(jLabel7)
 
                    .addComponent(jTextFieldCargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(jLabel2)
 
                    .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 
                .addGap(18, 18, 18)
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
                    .addComponent(ComboPeriodoSerie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(jFormattedTextFieldPeriodoSerie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(jLabel5)
 
                    .addComponent(jTextFieldTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 
                .addGap(18, 18, 18)
 
                .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
                    .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
                        .addComponent(jLabel3)
 
                        .addComponent(ComboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 
                    .addGroup(jPanelCamposLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
                        .addComponent(jLabel1)
 
                        .addComponent(ComboProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
 
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
        );
 

 
        tabelaDisciplina.setMaximumSize(new java.awt.Dimension(476, 253));
 
        tabelaDisciplina.setMinimumSize(new java.awt.Dimension(476, 253));
 
        tabelaDisciplina.setPreferredSize(new java.awt.Dimension(476, 253));
 

 
        jTableDisciplinas.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jTableDisciplinas.setModel(tabelaDisciplinas);
 
        jTableDisciplinas.getTableHeader().setReorderingAllowed(false);
 
        tabelaDisciplina.setViewportView(jTableDisciplinas);
 

 
        javax.swing.GroupLayout jPanelTelaLayout = new javax.swing.GroupLayout(jPanelTela);
 
        jPanelTela.setLayout(jPanelTelaLayout);
 
        jPanelTelaLayout.setHorizontalGroup(
 
            jPanelTelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelTelaLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addGroup(jPanelTelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
 
                    .addComponent(jPanelCampos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                    .addComponent(tabelaDisciplina, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
        );
 
        jPanelTelaLayout.setVerticalGroup(
 
            jPanelTelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelTelaLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addComponent(jPanelCampos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 
                .addComponent(tabelaDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                .addContainerGap())
 
        );
 

 
        jPanelBotoes.setLayout(new java.awt.GridBagLayout());
 

 
        jButtonSalvar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Add.png"))); // NOI18N
 
        jButtonSalvar.setText("Salvar");
 
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonSalvarActionPerformed(evt);
 
            }
 
        });
 
        jPanelBotoes.add(jButtonSalvar, new java.awt.GridBagConstraints());
 

 
        jButtonExcluir.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Delete.png"))); // NOI18N
 
        jButtonExcluir.setText("Excluir");
 
        jPanelBotoes.add(jButtonExcluir, new java.awt.GridBagConstraints());
        jButtonExcluir.addActionListener(new java.awt.event.ActionListener() {
        	 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonExcluirActionPerformed(evt);
 
            }
 
        });
 

 
        jButtonCancelar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Abort.png"))); // NOI18N
 
        jButtonCancelar.setText("Cancelar");
 
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonCancelarActionPerformed(evt);
 
            }
 
        });
 
        jPanelBotoes.add(jButtonCancelar, new java.awt.GridBagConstraints());
 

 
        jButtonListar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonListar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/List.png"))); // NOI18N
 
        jButtonListar.setText("Listar");
 
        jButtonListar.addActionListener(new java.awt.event.ActionListener() {
 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonListarActionPerformed(evt);
 
            }
 
        });
 
        jPanelBotoes.add(jButtonListar, new java.awt.GridBagConstraints());
 

 
        jButtonEditar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Application.png"))); // NOI18N
 
        jButtonEditar.setText("Editar");
 
        jButtonEditar.addActionListener(new java.awt.event.ActionListener() {
 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonEditarActionPerformed(evt);
 
            }
 
        });
 
        jPanelBotoes.add(jButtonEditar, new java.awt.GridBagConstraints());
 

 
        jButtonAtualizar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
 
        jButtonAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Load.png"))); // NOI18N
 
        jButtonAtualizar.setText("Atualizar");
 
        jButtonAtualizar.addActionListener(new java.awt.event.ActionListener() {
 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
                jButtonAtualizarActionPerformed(evt);
 
            }
 
        });
 
        jPanelBotoes.add(jButtonAtualizar, new java.awt.GridBagConstraints());
 

 
        javax.swing.GroupLayout jPanelDisciplinaLayout = new javax.swing.GroupLayout(jPanelDisciplina);
 
        jPanelDisciplina.setLayout(jPanelDisciplinaLayout);
 
        jPanelDisciplinaLayout.setHorizontalGroup(
 
            jPanelDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelDisciplinaLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addGroup(jPanelDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
 
                    .addComponent(jPanelTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
 
                    .addComponent(jPanelTela, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
                    .addComponent(jPanelBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
                .addContainerGap(32, Short.MAX_VALUE))
 
        );
 
        jPanelDisciplinaLayout.setVerticalGroup(
 
            jPanelDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
            .addGroup(jPanelDisciplinaLayout.createSequentialGroup()
 
                .addContainerGap()
 
                .addComponent(jPanelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
                .addComponent(jPanelTela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
 
                .addContainerGap(40, Short.MAX_VALUE))
 
        );
 

 
        add(jPanelDisciplina, "card2");
 
    }// </editor-fold>//GEN-END:initComponents
 

 
    @SuppressWarnings("unused")
 
    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {
 
        // TODO add your handling code here:
 
        if (jTextFieldCargaHoraria.getText().equals("") || jTextFieldNomeDisciplina.getText().equals("") || jTextFieldTurma.getText().equals("") || jFormattedTextFieldPeriodoSerie.getText().equals("")) {
 
            JOptionPane.showMessageDialog(null, "Obrigatório preencher todos os campos");
 
        } else {
 
            Disciplina disciplina = null;
 
            try {
 
                int idDisciplina = 1;
 
                ControladorDisciplina cd = new ControladorDisciplina();
 
                Disciplina d;
 
                while (true) {
 
                    d = cd.ConsultarDisciplina(idDisciplina);
 
                    if (d != null) {
 
                        idDisciplina += 1;
 
                    } else {
 
                        d = new Disciplina();
 
                        d.setCodDisciplina(idDisciplina);
 
                        d.setCarga_horaria(Integer.parseInt(jTextFieldCargaHoraria.getText()));
 
                        d.setNome(jTextFieldNomeDisciplina.getText());
 
                        d.setCurso(cursos.get(ComboCurso.getSelectedIndex()));
 
                        d.setTurma(jTextFieldTurma.getText());
 
                        d.setAno(2013);
                        
                        ArrayList<Professor> ps = new ArrayList<>();
                        ps.add(professores.get(ComboProfessor.getSelectedIndex()));
                        
                        d.setProfessores(ps);
                        
 
                        if (ComboPeriodoSerie.getSelectedIndex() == 0) {
 
                            d.setPeriodo(jFormattedTextFieldPeriodoSerie.getText());
 
                        } else {
 
                            d.setSerie(Integer.parseInt(jFormattedTextFieldPeriodoSerie.getText()));
 
                        }
 
                        cd.SalvarDisciplina(d);
 
                        limparCampos();
 
                        preencheTabela();
 
                        break;
 
                    }
 
                }
 
            } catch (NumberFormatException ex) {
 
                JOptionPane.showMessageDialog(null, "Informe os dados corretos!");
 
            }
 
        }
 
    }
 

 
    private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {
 
        int delete;
 
        if (jTableDisciplinas.getSelectedRowCount() == 0) {
 
            JOptionPane.showMessageDialog(null,
 
                    "Clique na linha onde encontra-se a disciplina que deseja excluir!");
 
        } else {
 
            int linha = jTableDisciplinas.getSelectedRow();
 
            ControladorDisciplina cd = new ControladorDisciplina();
 
            jTextFieldNomeDisciplina.setText(jTableDisciplinas.getValueAt(linha, 2) + "");
 
            jTextFieldCargaHoraria.setText(jTableDisciplinas.getValueAt(linha, 3) + "");
 
            jTextFieldTurma.setText(jTableDisciplinas.getValueAt(linha, 4) + "");
 

 
            delete = (int) jTableDisciplinas.getValueAt(linha, 0);
 
            int opcao = JOptionPane.showConfirmDialog(null,
 
                    "Tem certeza que deseja excluir?", "Excluir!",
 
                    JOptionPane.YES_NO_OPTION);
 

 
            if (opcao == JOptionPane.YES_OPTION) {
 
                try {
 
                    cd.ExcluirDisciplina(delete);
 
                    limparCampos();
 
                    preencheTabela();
 
                } catch (Exception e) {
 
                    JOptionPane.showMessageDialog(null,
 
                            "Está disciplina está vinculada a algum curso, desvincule-a para realizar a exclusão!");
 
                    limparCampos();
 
                    preencheTabela();
 
                }
 
            } else {
 
                JOptionPane.showMessageDialog(null, "Exclusão cancelada!");
 
                limparCampos();
 
            }
 
        }
 
    }
 

 
    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
 
        preencheTabela();
 
        limparCampos();
 
    }
 

 
    private void jButtonListarActionPerformed(java.awt.event.ActionEvent evt) {
 
        preencheTabela();
 
    }
 

 
    private void jButtonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarActionPerformed
 
        // TODO add your handling code here:
 
        if (jTableDisciplinas.getSelectedRowCount() == 0) {
 
            JOptionPane.showMessageDialog(null, "Selecione a linha onde encontra-se a disciplina que deseja editar!");
 
        } else {
 
            int linha = jTableDisciplinas.getSelectedRow();
 
            ControladorDisciplina cd = new ControladorDisciplina();
 
            Disciplina d = cd.ConsultarDisciplina(Integer.parseInt(jTableDisciplinas.getValueAt(linha, 0).toString()));
 
            altera = Integer.parseInt(jTableDisciplinas.getValueAt(linha, 0).toString());
 
            jTextFieldNomeDisciplina.setText(jTableDisciplinas.getValueAt(linha, 2).toString());
 
            jTextFieldCargaHoraria.setText(jTableDisciplinas.getValueAt(linha, 3).toString());
 
            jTextFieldTurma.setText(jTableDisciplinas.getValueAt(linha, 4).toString());
 
            if (jTableDisciplinas.getValueAt(linha, 5) != null) {
 
                jFormattedTextFieldPeriodoSerie.setText(jTableDisciplinas.getValueAt(linha, 5).toString());
 
            } else {
 
                jFormattedTextFieldPeriodoSerie.setText(jTableDisciplinas.getValueAt(linha, 7).toString());
 
            }
 
            jButtonAtualizar.setEnabled(true);
 
        }
 

 

 
    }//GEN-LAST:event_jButtonEditarActionPerformed
 

 
    private void ComboPeriodoSerieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboPeriodoSerieActionPerformed
 
        // TODO add your handling code here:
 
    }//GEN-LAST:event_ComboPeriodoSerieActionPerformed
 

 
    private void jButtonAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAtualizarActionPerformed
 
        Disciplina d = null;
 
        int num;
 
        if (jTableDisciplinas.getSelectedRowCount() == 0) {
 
            JOptionPane.showMessageDialog(null,
 
                    "Clique e/ou Pesquise pelo curso que deseja atualizar!");
 
        } else {
 
            d = new Disciplina();
 
            ControladorDisciplina cd = new ControladorDisciplina();
 
            d.setCodDisciplina(altera);
 
            d.setCarga_horaria(Integer.parseInt(jTextFieldCargaHoraria.getText()));
 
            d.setNome(jTextFieldNomeDisciplina.getText());
 
            d.setCurso(cursos.get(ComboCurso.getSelectedIndex()));
 
            d.setTurma(jTextFieldTurma.getText());
 
            d.setAno(2013);
 
            //d.setMatricula(1);
 
            if (ComboPeriodoSerie.getSelectedIndex() == 0) {
 
                d.setPeriodo(jFormattedTextFieldPeriodoSerie.getText());
 
            } else {
 
                d.setSerie(Integer.parseInt(jFormattedTextFieldPeriodoSerie.getText()));
 
            }
 

 
            int opcao = JOptionPane.showConfirmDialog(null,
 
                    "Tem certeza que deseja alterar?", "Alterar!",
 
                    JOptionPane.YES_NO_OPTION);
 

 
            if (opcao == JOptionPane.YES_OPTION) {
 
                try {
 
                    cd.SalvarDisciplina(d);
 
                    limparCampos();
 
                    preencheTabela();
 
                } catch (Exception e) {
 
                    JOptionPane.showMessageDialog(null,
 
                            "Não existe curso com esse código!");
 
                    limparCampos();
 
                    preencheTabela();
 
                }
 
            } else {
 
                JOptionPane.showMessageDialog(null, "Exclusão cancelada!");
 
                limparCampos();
 
            }
 
        }
 
        altera = 0;
 
    }//GEN-LAST:event_jButtonAtualizarActionPerformed
 

 
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
    private javax.swing.JComboBox ComboCurso;
 
    private javax.swing.JComboBox ComboPeriodoSerie;
 
    private javax.swing.JComboBox ComboProfessor;
 
    private javax.swing.ButtonGroup buttonGroupPeriodoSerie;
 
    private javax.swing.JButton jButtonAtualizar;
 
    private javax.swing.JButton jButtonCancelar;
 
    private javax.swing.JButton jButtonEditar;
 
    private javax.swing.JButton jButtonExcluir;
 
    private javax.swing.JButton jButtonListar;
 
    private javax.swing.JButton jButtonSalvar;
 
    private javax.swing.JFormattedTextField jFormattedTextFieldPeriodoSerie;
 
    private javax.swing.JLabel jLabel1;
 
    private javax.swing.JLabel jLabel2;
 
    private javax.swing.JLabel jLabel3;
 
    private javax.swing.JLabel jLabel4;
 
    private javax.swing.JLabel jLabel5;
 
    private javax.swing.JLabel jLabel7;
 
    private javax.swing.JPanel jPanelBotoes;
 
    private javax.swing.JPanel jPanelCampos;
 
    private javax.swing.JPanel jPanelDisciplina;
 
    private javax.swing.JPanel jPanelTela;
 
    private javax.swing.JPanel jPanelTitulo;
 
    private javax.swing.JTable jTableDisciplinas;
 
    private javax.swing.JTextField jTextFieldCargaHoraria;
 
    private javax.swing.JTextField jTextFieldNomeDisciplina;
 
    private javax.swing.JTextField jTextFieldTurma;
 
    private javax.swing.JScrollPane tabelaDisciplina;
 
    // End of variables declaration//GEN-END:variables
 

 
    private void preencheTabela() {
 
        tabelaDisciplinas.setNumRows(0);
 
        ControladorDisciplina cd = new ControladorDisciplina();
 
        List<Disciplina> lista = cd.ListarDisciplinas();
 
        int t = lista.size();
 
        for (int i = 0; i < t; i++) {
 
            Disciplina disciplina = lista.get(i);
 
            tabelaDisciplinas.addRow(new Object[]{disciplina.getCodDisciplina(), disciplina.getCurso().getNome(), disciplina.getNome(), disciplina.getCarga_horaria(), disciplina.getTurma(), disciplina.getPeriodo(), disciplina.getAno(), disciplina.getSerie()});
 
        }
 
    }
 

 
    private void limparCampos() {
 
        jTextFieldNomeDisciplina.setText("");
 
        jTextFieldCargaHoraria.setText("");
 
        jTextFieldTurma.setText("");
 
        jFormattedTextFieldPeriodoSerie.setText("");
 
        jButtonAtualizar.setEnabled(false);
 
    }
 
}
 



